package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//购物车
@RestController
public class CartController {
    //注入service
    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("cart/add")
    public ResultInfo add(@RequestBody Cart cartParm) { //1.接收参数
        //调用service添加
        Cart cart = cartService.add(cartParm);
        //调用service添加并返回
        return ResultInfo.success(cart);
    }

    //查询购物车
    @GetMapping("/cart/list")
    public ResultInfo list() {
        //调用service查询
        // List<Cart> cartList =  cartService.list();
        //调用service查询并返回
        return ResultInfo.success(cartService.list());
        //return ResultInfo.success(cartList);
    }

     //修改购物车
     @PostMapping("/cart/sub")
     public ResultInfo sub(@RequestBody Cart cartParm){ //接收参数
         //调用service并返回
         return ResultInfo.success(cartService.sub(cartParm));

     }
    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean() {
        //调用service清空
        cartService.clean();
        //返回
        return ResultInfo.success();
    }

}
